// components/pulldown-refresh/pulldown-refresh.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    top: {
      type: String,
      value: '82rpx'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    startPage: 1,
    page: 1,


    // 只会处理 none 的状态
    refreshStatus: '',
    // 到底加载状态
    //  loading 加载中 finish 完成(没有更多了) none 没有数据
    lowerStatus: '',    
    //
    refresherTriggered: false
  },

  lifetimes: {
    attached: function () {
      this.refresh()
    },
    moved: function () { },
    detached: function () { },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    refresh() {
      let page = this.data.startPage
      this.setData({ page })
      this.setData({ lowerStatus: '' })
      this.triggerEvent('load', {
        page,
        success: states => {
          this.setData({
            refreshStatus: states,
            refresherTriggered: false
          })
        },
        complete: () => { }
      })
    },
    onLower() {
      let page = this.data.page + 1
      this.setData({ page })
      let lowerStatus = this.data.lowerStatus
      if (this.lowerLoading || lowerStatus === 'finish' || lowerStatus === 'none') return
      this.lowerLoading = true
      this.setData({ lowerStatus: 'loading' })
      this.triggerEvent('load', {
        page,
        success: states => {
          this.setData({
            lowerStatus: states
          })
        },
        complete: () => {
          this.lowerLoading = false
        }
      })
    },
  }
})
